interface NowDateOptions {
  template?: string;
}

export function useTime(options: NowDateOptions = {}) {
  const { template = "YYYY年MM月DD dddd HH:mm:ss" } = options;

  const value = ref(createDate());
  let timer: NodeJS.Timeout | null = null;

  function start() {
    timer = setInterval(updateDate, 1000)
  }

  function stop() {
    if (timer) {
      clearInterval(timer);
      timer = null
    }
  }

  function createDate() {
    return dayjs().format(template);
  }

  function updateDate() {
    value.value = createDate();
  }

  return proxyRefs({
    start,
    stop,
    value,
  });
}
